package com.wolf.controller;

import com.wolf.pojo.Wl_user_role_competences;
import com.wolf.service.Wl_user_roleService;
import com.wolf.service.Wl_user_role_competencesService;
import com.wolf.util.Math;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ComponentScan
public class Wl_user_role_competencesController {

    @Autowired
    private Wl_user_role_competencesService wl_user_role_competencesService;

    @Autowired
    private Wl_user_roleService wl_user_roleService;

    public void setWl_user_roleService(Wl_user_roleService wl_user_roleService) {
        this.wl_user_roleService = wl_user_roleService;
    }

    public void setWl_user_role_competencesService(Wl_user_role_competencesService wl_user_role_competencesService) {
        this.wl_user_role_competencesService = wl_user_role_competencesService;
    }

    @RequestMapping("wl_user_role_competences_getbyid")
    public String getbyid(HttpServletRequest request, String id){

        List<Wl_user_role_competences> list = wl_user_role_competencesService.getbyid(id);

        request.getSession().setAttribute("wl_user_role_bean",wl_user_roleService.selectByPrimaryKey(id));

        request.getSession().setAttribute("wl_user_role_competences_list",list);

        return "redirect:wl_user_competence_getall";
    }

    /**
     * 新增或修改
     * @return
     */
    @RequestMapping("wl_user_competence_insertordelete")
    @ResponseBody
    public int insertordelete(String roleid,String userid){

        Wl_user_role_competences wl_user_role_competences =wl_user_role_competencesService.getbyroleid(roleid,userid);

        if(wl_user_role_competences!=null){
            wl_user_role_competencesService.deleteByPrimaryKey(wl_user_role_competences.getId());
            return 0;
        }

        Wl_user_role_competences wl = new Wl_user_role_competences();
        wl.setId(Math.random());
        wl.setRoleid(roleid);
        wl.setCompetenceid(userid);

        wl_user_role_competencesService.insert(wl);

        return 1;
    }
}
